/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player.abilities;

import com.cmp.arena.controls.BallControl;
import static com.cmp.core.constants.InputUIDConstants.ABILITY_BUTTON_UID;
import static com.cmp.core.constants.InputUIDConstants.CURSOR_POS_UID;
import static com.cmp.core.constants.UIDConstants.*;
import com.cmp.player.controls.CharacterRigidBodyControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 * Ability for throwing the ball.
 * @author krummer
 */
public class ThrowControl extends AbilityControl {
    private static final float THROW_SPEED = 15.f;
    private boolean isAbility;
    private Node ball;
    private CharacterRigidBodyControl body;
    
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        super.setCooldown(0.f);
        
        spatial.setUserData(ABILITY_BUTTON_UID, false);
        this.body = spatial.getControl(CharacterRigidBodyControl.class);
    }

    @Override
    public void execute() {
        if(!this.isOnCooldown()) {

            ball = (Node) ((Node)this.spatial).getChild(BALL_UID);
            if(ball != null) {
                super.activateCooldown();
                super.activateGCD();
                
                CharacterRigidBodyControl ballBody;
                ballBody = ball.getControl(CharacterRigidBodyControl.class);

                if(super.getSpatial().getUserData(CURSOR_POS_UID) != null) {
                    ball.getControl(BallControl.class).attachToMap();
                    Vector3f targetPos = ((Vector3f)super.getSpatial().getUserData(CURSOR_POS_UID)).clone();
                    Vector3f ballPos = ball.getWorldTranslation().clone();
                    
                    
                    // throw ball at mouse pos.
                    ballBody.setLinearVelocity(calculateVerticalLaunchSpeed(ballBody.getGravity().y, THROW_SPEED, ballPos, targetPos));
                }
            } 
        }
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        super.controlUpdate(tpf);
        
        isAbility = super.getSpatial().getUserData(ABILITY_BUTTON_UID);
        if(isAbility) {
            execute();
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
    
    private Vector3f calculateVerticalLaunchSpeed(float gravity, float verticalLaunchSpeed, Vector3f launchPoint, Vector3f target) {
        Vector3f dir = target.subtract(launchPoint);
        float height = dir.y;
        dir.y = 0;
        float distance = dir.length();
        dir.normalizeLocal();
        Vector3f launchSpeed = dir.mult(verticalLaunchSpeed);
        launchSpeed.y = (height - gravity / 2.f 
                * (distance / verticalLaunchSpeed) 
                * (distance / verticalLaunchSpeed))
                * verticalLaunchSpeed / distance;
        return launchSpeed;
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
